package io.renren.modules.base.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.common.annotation.SysLog;
import io.renren.modules.base.dao.BaseWorkitemInfoDao;
import io.renren.modules.base.entity.BasePhaseInfoEntity;
import io.renren.modules.base.entity.BaseTaskInfoEntity;
import io.renren.modules.base.entity.BaseTemplateEntity;
import io.renren.modules.base.entity.BaseWorkitemInfoEntity;
import io.renren.modules.base.service.BasePhaseInfoService;
import io.renren.modules.base.service.BaseTaskInfoService;
import io.renren.modules.base.service.BaseTemplateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;




/**
 * 
 * queryTask
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-10-05 17:49:18
 */
@RestController
@RequestMapping("/generator/basetaskinfo")
public class BaseTaskInfoController {
	@Autowired
	private BaseTaskInfoService baseTaskInfoService;
	@Autowired
	private BasePhaseInfoService basePhaseInfoService;
	@Autowired
	private BaseTemplateService baseTemplateService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("generator:basetaskinfo:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<BaseTaskInfoEntity> baseTaskInfoList = baseTaskInfoService.queryList(query);
		int total = baseTaskInfoService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(baseTaskInfoList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 全查询
	 */
	@RequestMapping("/allList")
	public R allList(){
		return R.ok().put("list", baseTaskInfoService.allList());
	}
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("generator:basetaskinfo:info")
	public R info(@PathVariable("id") String id){
		BaseTaskInfoEntity baseTaskInfo = baseTaskInfoService.queryObject(id);
		return R.ok().put("baseTaskInfo", baseTaskInfo);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/queryObjectInfo/{id}")
	public R queryObjectInfo(@PathVariable("id") String id){
		BaseTaskInfoEntity baseTaskInfo = baseTaskInfoService.queryObjectInfo(id);
		return R.ok().put("baseTaskInfo", baseTaskInfo);
	}
	
	/**
	 * 保存
	 */
	@SysLog("模板新增任务")
	@RequestMapping("/save")
	@RequiresPermissions("generator:basetaskinfo:save")
	public R save(@RequestBody BaseTaskInfoEntity baseTaskInfo){
		baseTaskInfoService.save(baseTaskInfo);
		
		return R.ok();
	}

	@SysLog("模板新增任务")
	@RequestMapping("/add/{taskName}/{phaseId}/{templateId}")
	public R add(@PathVariable("taskName") String taskName,@PathVariable("phaseId") String phaseId,@PathVariable("templateId") String templateId){
		BaseTaskInfoEntity baseTaskInfoEntity = new BaseTaskInfoEntity();
		baseTaskInfoEntity.setPhaseId(phaseId);
		baseTaskInfoEntity.setTemplateId(templateId);
		baseTaskInfoEntity.setTaskName(taskName);
		baseTaskInfoService.save(baseTaskInfoEntity);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("模板修改任务")
	@RequestMapping("/update/{id}/{taskName}/{phaseId}")
	public R update(@PathVariable("id") String id, @PathVariable("taskName") String taskName,@PathVariable("phaseId") String phaseId){
		System.out.println(id);
		BaseTaskInfoEntity baseTaskInfo = new BaseTaskInfoEntity();
		baseTaskInfo.setId(id);
		baseTaskInfo.setTaskName(taskName);
		baseTaskInfo.setPhaseId(phaseId);
		System.out.println(baseTaskInfo);
		baseTaskInfoService.updateTask(baseTaskInfo);
		
		return R.ok();
	}

	/**
	 * 修改任务
	 */
	@SysLog("修改任务")
	@RequestMapping("/updateTaskInfo")
	public R updateTaskInfo(@RequestBody BaseTaskInfoEntity baseTaskInfo){
		baseTaskInfoService.updateTask(baseTaskInfo);

		return R.ok();
	}

	/**
	 * 修改模板任务
	 */
	@SysLog("修改任务")
	@RequestMapping("/update")
	public R update(@RequestBody BaseTaskInfoEntity baseTaskInfo){
		baseTaskInfoService.updateBase(baseTaskInfo);

		return R.ok();
	}
	
	/**
	 * 删除updateOrder
	 */
	@SysLog("删除任务")
	@RequestMapping("/delete")
	@RequiresPermissions("generator:basetaskinfo:delete")
	public R delete(@RequestBody String[] ids){
		baseTaskInfoService.deleteBatch(ids);
		
		return R.ok();
	}

	@SysLog("删除任务")
	@RequestMapping("/deleteTask/{taskId}/{templateId}")
    public R deleteTask(@PathVariable("taskId") String taskId,@PathVariable("templateId") String templateId,@RequestBody List<BaseTaskInfoEntity> baseTaskInfoEntities){
	    baseTaskInfoService.deleteTask(taskId,templateId,baseTaskInfoEntities);
	   return R.ok();
    }

    //根据模板ID查询TASK
	@RequestMapping("/queryTask/{templateId}")
	@RequiresPermissions("generator:basetaskinfo:template")
	public R queryTask(@PathVariable("templateId") String templateId){
		List list = null;
		if (!"".equals(templateId)){
			list = baseTaskInfoService.queryTask(templateId);
		}
		List<BasePhaseInfoEntity>  phaseList = basePhaseInfoService.allList();
		List<BaseTemplateEntity> templateList = baseTemplateService.query();
		R r = new R();
		r.put("list",list);
		r.put("phaseList",phaseList);
		r.put("templateList",templateList);
		return r;
	}

	@RequestMapping("/queryTemplateTask/{templateId}")
	public R queryTemplateTask(@PathVariable("templateId") String templateId){
         List list = baseTaskInfoService.queryTemplateTask(templateId);
		R r = new R();
		r.put("list",list);
		return r;
	}

	//往模板里面添加任务
	@SysLog("往模板里面添加任务")
	@RequestMapping("/templateAddTask")
	public R templateAddTask(@RequestBody List<BaseTaskInfoEntity> baseTaskInfoEntities){
		System.out.println(baseTaskInfoEntities);
		baseTaskInfoService.templateAddTask(baseTaskInfoEntities);

		return R.ok();
	}

	@RequestMapping("/updateOrder")
	public R updateOrder(@RequestBody List<BaseTaskInfoEntity> baseTaskInfoEntities){
		baseTaskInfoService.updateOrder(baseTaskInfoEntities);
		return R.ok();
	}
	
}
